import java.io.*;
import java.net.*;
import java.util.Date;

public class MyServer {

	public static int LISTENING_PORT = 3333;

	public static void main(String[] args)
	
		throws IOException {
			ServerSocket serverSocket =	new ServerSocket(LISTENING_PORT);
			System.out.println("Server started.");
		
			while (true) {
				Socket socket = serverSocket.accept();
				DictionaryClientThread readThread =	new DictionaryClientThread(socket);
				readThread.start();
			}
		}
	}

class DictionaryClientThread extends Thread {
	
	private int CLIENT_REQUEST_TIMEOUT = 15*60*1000;
	private Socket mSocket;
	private BufferedReader mSocketReader;
	private PrintWriter mSocketWriter;
	public DictionaryClientThread(Socket aSocket)
	throws IOException {
	mSocket = aSocket;
	mSocket.setSoTimeout(CLIENT_REQUEST_TIMEOUT);
	mSocketReader = new BufferedReader(
	new InputStreamReader(
	mSocket.getInputStream()));
	mSocketWriter = new PrintWriter(
	new OutputStreamWriter(
	mSocket.getOutputStream()));
	}

	public void run() {
		System.out.println("Accepted new client.");
		try {
		mSocketWriter.println("Server ready.");
		mSocketWriter.flush();
		while (!isInterrupted()) {
		String name = mSocketReader.readLine();
		if (name == null)
		break; // Client closed the socket
		String translation = getFile(name);
		mSocketWriter.println(translation);
		mSocketWriter.flush();
		}
		} catch (Exception ex) {
		ex.printStackTrace();
		}
		System.out.println("Connection lost.");
		}	

	private String getFile(String a) {
		
		try {
			RandomAccessFile raf = new RandomAccessFile(a, "r");
			
			try {
				return raf.readLine();
			} catch (IOException e) {
				return "Error reading file "+a;
			}
		} catch (FileNotFoundException e) {
			return "Error reading file "+a;
		}
		
		//return "";
	
	}
	
}	
	